<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>购物车</title>
    <script src="../js/vue.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        .table {
            width: 700px;
            margin: 50px auto;
        }

        .table .tr .item {
            width: 100px;
            height: 50px;
            line-height: 50px;
            text-align: center;
            background-color: black;
            color: coral;
            float: left;
        }

        .table .tr .item input {
            width: 50px;
            text-align: center;
        }

        .bigbox {
            width: 200px !important;
        }
    </style>
</head>

<body>

    <div id="app">
        <div class="table">
            <div class="tr">
                <div class="item"><input type="checkbox" :checked="checkedall" @change="setcheckedall">全选</div>
                <div class="item">名称</div>
                <div class="item">价格</div>
                <div class="item">数量</div>
                <div class="item bigbox">小计</div>
                <div class="item">操作</div>
            </div>

            <div class="tr" v-for="item in goodslist">
                <div class="item"><input type="checkbox" :value="item.id" v-model="item.checked"></div>
                <div class="item">{{item.goodsname}}</div>
                <div class="item">{{(item.goodsprice*1).toFixed(2)}}</div>
                <div class="item">
                    <button v-show="item.goodsnum>1" @click="calc(item,-1)">-</button>
                    <input :value="item.goodsnum">
                    <button @click="calc(item,1)">+</button>
                </div>
                <div class="item bigbox">{{(item.goodsprice*item.goodsnum*1).toFixed(2)}}</div>
                <div class="item" @click="del(item.id)">删除</div>
            </div>

            <div class="tr">
                <div class="item" @click="delall">
                    批量删除
                </div>
                <div class="item">

                </div>
                <div class="item">

                </div>
                <div class="item">
                    总数量:{{num}}
                </div>
                <div class="item bigbox">
                    总价格:{{price.toFixed(2)}}
                </div>
                <div class="item">结算
                </div>
            </div>

        </div>

    </div>


</body>

<script>

    new Vue({
        el: "#app",

        data: {

            checkedall: false,

            goodslist: [
                { id: 1, goodsname: "第一件", goodsprice: 100, goodsnum: 1, checked: false },
                { id: 2, goodsname: "第二件", goodsprice: 200, goodsnum: 2, checked: false },
                { id: 3, goodsname: "第三件", goodsprice: 300, goodsnum: 3, checked: false },
                { id: 4, goodsname: "第四件", goodsprice: 400, goodsnum: 4, checked: false },
                { id: 5, goodsname: "第五件", goodsprice: 500, goodsnum: 5, checked: false },
                { id: 6, goodsname: "第六件", goodsprice: 600, goodsnum: 6, checked: false },
            ],

            // num: 1,
            // price: 100,
        },

        computed: {//计算属性  补充data的数据  data有以data为主
            num() {
                let checkedList = this.goodslist.filter(item => item.checked);
                if (checkedList.length) {
                    return checkedList.map(item => item.goodsnum).reduce((a, b) => a + b);
                }
                return 0;
            },
            price() {
                let checkedList = this.goodslist.filter(item => item.checked);
                if (checkedList.length) {
                    return checkedList.map(item => item.goodsnum * item.goodsprice).reduce((a, b) => a + b);
                }
                return 0;
            }
        },

        methods: {

            setcheckedall(e) {
                this.goodslist.forEach(item => {
                    item.checked = e.target.checked;
                })
            },

            del(id) {
                this.goodslist = this.goodslist.filter(item => item.id != id);
            },

            delall() {
                this.goodslist = this.goodslist.filter(item => !item.checked);
            },

            calc(info, num) {
                info.goodsnum = info.goodsnum + num;
            }

        },

        watch: {

            goodslist: {
                deep: true,
                handler(newList) {
                    // console.log(newList);
                    this.checkedall = newList.every(item => item.checked);
                }
            },

        }

    })




</script>

</html>